﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using MayhemCore;
using MayhemWpf.ModuleTypes;
using MayhemWpf.UserControls;
using System.Windows.Threading;
using Total_Mayhem.Common;

namespace Total_Mayhem.Power
{
    [DataContract]
    [MayhemModule("Turn Monitor On/Off", "Set monitor power state to on/off/standby")]
    public class MonitorPowerReactions : ReactionBase, IWpfConfigurable
    {
        #region Initialization Code
        protected override void OnLoadDefaults()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnLoadFromSaved()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnBeforeLoad()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnAfterLoad()
        {
            Misc.WriteLog("Entered");
        }

        protected override void OnDeleted()
        {
            Misc.WriteLog("Entered");
        }
        #endregion

        [DataMember]
        public string NirCmd { get; set; }
        
        protected override void OnEnabling(EnablingEventArgs e)
        {
            //base.OnEnabling(e);
            Misc.WriteLog("Entered");
        }

        protected override void OnDisabled(DisabledEventArgs e)
        {
            //base.OnDisabled(e);
            Misc.WriteLog("Entered");
        }

        public WpfConfiguration ConfigurationControl
        {
            get
            {
                Misc.WriteLog("OpenConfig");
                return new MonitorPowerReactionsConfig(){NirCmd = NirCmd};
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            MonitorPowerReactionsConfig c = configurationControl as MonitorPowerReactionsConfig;
            if (c != null)
            {
                Misc.WriteLog("Settings: NirCmd={0}", c.NirCmd);
                NirCmd = c.NirCmd;
            }
        }

        /// <summary>
        /// Used to generate the text that is displayed on the main page of events/triggers to describe this entry
        /// </summary>
        /// <returns></returns>
        public string GetConfigString() { return NirCmd; }//string.Format("Power State={0}", this.strPowerState); }

        public override void Perform()
        {
            Misc.WriteLog("Entered");
            string strArgs = NirCmd.Replace("nircmd.exe ", "");
            Misc.ExecuteNirCmd(strArgs);
        }
    }
}
